//
//  EnterpriseProgressEnquiryViewController.m
//  homeland
//
//  Created by SunShine on 13-11-2.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "EnterpriseProgressEnquiryViewController.h"
#import "AdvisoryListCell.h"
#import "EnterpriseProgressDetailViewController.h"
#import "UserInfoService.h"

@interface EnterpriseProgressEnquiryViewController ()
@property (strong, nonatomic) CustomPullToRefresh *ptr;
@property (strong, nonatomic) ProgressEnquiryService *dataService;
@end

@implementation EnterpriseProgressEnquiryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"企业办事查询";
    _searchBar.inputAccessoryView = [self getInputAccessoryView];
    self.dataService = [ProgressEnquiryService new];
    _dataService.delegate = self;
    //开启下拉刷新
    self.ptr = [[CustomPullToRefresh alloc]initWithScrollView:self.tableView delegate:self];
    //底部消息
    if ([UserInfoService getUserInfoService].currentLoginUserName) {
        _tipsView.text = @"如果您在个人电脑上提交的记录没有在此显示，请输入当时的业务流水号查询";
    } else {
        _tipsView.text = @"您未登录，请输入业务流水号查询";
    }
//    self.statusView.hidden=YES;//默认不显示
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataService.dataList count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EnterpriseProgressListItem *item = [_dataService.dataList objectAtIndex:indexPath.row];
    AdvisoryListCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"AdvisoryListCell" owner:self options:nil] objectAtIndex:0];
    cell.codeLabel.text = item.user_id;
    cell.statusLabel.text = item.current_link;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    EnterpriseProgressDetailViewController *detailVC = [[EnterpriseProgressDetailViewController alloc] initWithNibName:@"EnterpriseProgressDetailViewController" bundle:nil];
    detailVC.listItem = [_dataService.dataList objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - Search Bar Delegate
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSLog(@"search bar input:%@",searchText);
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"search button clicked with input:%@",searchBar.text);
    [searchBar resignFirstResponder];
}

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    NSLog(@"search bar end edit with input %@",searchBar.text);
    [self startLoadingView];
    if (searchBar.text.length == 0) {
        // do sth
    } else {
        // do sth
        [_dataService requestEnterprisePeogressWithKey:searchBar.text];
        self.statusView.hidden=NO;//查询的时候显示
    }
}

#pragma mark - PullToRefresh Method
//捕获到拉动事件
-(void)customPullToRefreshShouldRefresh:(CustomPullToRefresh *)ptr withDirection:(MSRefreshDirection)direction
{
    if (direction == MSRefreshDirectionTop) {
        if (self.searchBar.text == 0) {
//            [self.dataService loadDataWithKey:nil];
        } else {
            [_dataService requestEnterprisePeogressWithKey:_searchBar.text];
        }
    }
    if (direction == MSRefreshDirectionBottom) {
        if (self.searchBar.text == 0) {
//            [self.dataService loadMoreDataWithKey:nil];
        } else {
            [_dataService requestMoreEnterprisePeogressWithKey:_searchBar.text];
        }
    }
}

#pragma mark - Data Service
-(void)loadDataFinished
{
    [self stopLoadingView];
    [self.ptr endRefresh];
    [self.tableView reloadData];
}

-(void)loadDataFaild:(NSString *)msg
{
    [self stopLoadingView];
    [self.ptr endRefresh];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"错误" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

-(void)setEnableToLoadMore:(BOOL)enable
{
    self.ptr.enablePullToBottom = enable;
}
@end
